﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Mst;
using ESOL.Business.Fin;
using ESOL.Common;
using ESOL.Common.Entity;
using ESOL.WinUI.MasterData;
using ESOL.WinUI.Hr;

namespace ESOL.WinUI.Finance
{
    public partial class frmManageAccountingVoucher : BaseForm.frmBaseMdiBusinessForm
    {
        #region Variable
        private FinAccountingVoucherEntity mSelectedAccountingVoucher;
        #endregion

        #region Constructor
        public frmManageAccountingVoucher()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "STATUS", true);

            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colSTATUS, "STATUS", true);
        }
        private string GetCondition()
        {
            string condition = "";
            if (txtCreatedBy.Tag != null && txtCreatedBy.Tag.GetType() == typeof(MstPersonEntity))
            {
                MstPersonEntity entity = (MstPersonEntity)txtCreatedBy.Tag;
                condition += " AND AV.CREATED_BY = '" + entity.PersonId + "' ";
            }
            if (string.IsNullOrEmpty(cmbStatus.Text) == false && cmbStatus.SelectedValue != null)
            {
                condition += " AND AV.STATUS = '" + cmbStatus.SelectedValue.ToString() + "'";
            }
            if (string.IsNullOrEmpty(txtVoucherNo.Text) == false)
            {
                condition += " AND AV.VOUCHER_NO LIKE '%" + txtVoucherNo.Text + "%'";
            }
            if (dtpAccountingDate.EditValue!=null)
            {
                condition += " AND AV.ACCOUNTING_DATE >= CONVERT(DATETIME,'" + dtpAccountingDate.EditValue.ToString() + "')";
            }
            if (dtpAccountingDateTo.EditValue != null)
            {
                condition += " AND AV.ACCOUNTING_DATE <= CONVERT(DATETIME,'" + dtpAccountingDateTo.EditValue.ToString() + "')";
            }
            return condition;
        }
        private void BindDataGrid()
        {
            string condition = GetCondition();
            string rowCount = txtRowCount.Text;
            string orderBy = "AV.VOUCHER_NO DESC";
            DataTable dt = FinAccountingVoucherBs.GetByCondition(condition, orderBy, rowCount);
            gvMain.DataSource = dt;
        }
        private void BindDetailGrid(string voucherId)
        {
            if (string.IsNullOrEmpty(voucherId))
            {
                gvDetail.DataSource = null;
                return;
            }
            DataTable dt = FinAccountingVoucherItemBs.GetDataTableByVoucherID(voucherId);
            if (dt == null)
            {
                gvDetail.DataSource = null;
            }
            gvDetail.DataSource = dt;
        }
        private void ResetButtonsEnable()
        {
            btnModify.Enabled = false;
            btnDelete.Enabled = false;
            btnSubmit.Enabled = false;
            btnApprove.Enabled = false;
            if (mSelectedAccountingVoucher == null)
            {
                return;
            }
            if (mSelectedAccountingVoucher.Status == NormalStatus.STATUS01.ToString()
                || mSelectedAccountingVoucher.Status == NormalStatus.STATUS02.ToString()
                || mSelectedAccountingVoucher.Status == NormalStatus.STATUS04.ToString())
            {
                btnModify.Enabled = true;
                btnDelete.Enabled = true;
                btnApprove.Enabled = true;
            }
            else if (mSelectedAccountingVoucher.Status == NormalStatus.STATUS03.ToString())
            {
            }
        }
        #endregion

        #region Event
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            BindComboBox();
            BindDataGrid();
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mSelectedAccountingVoucher = new FinAccountingVoucherEntity(dr);
            BindDetailGrid(mSelectedAccountingVoucher.VoucherId);
            ResetButtonsEnable();
        }

        protected override void Click_View()
        {
            if (mSelectedAccountingVoucher == null)
            {
                MessageBox.Show("请选择一个记账凭证！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainAccountingVoucher frm = new frmMaintainAccountingVoucher();
            frm.ThisActionType = ActionType.View;
            frm.SelectedAccountingVoucher = mSelectedAccountingVoucher;
            frm.ShowDialog();
        }

        protected override void Click_Delete()
        {
            if (mSelectedAccountingVoucher == null)
            {
                MessageBox.Show("请选择一个记账凭证！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("是否删除选中记账凭证？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                mSelectedAccountingVoucher.Status = NormalStatus.STATUS05.ToString();
                mSelectedAccountingVoucher.LastModifiedBy = CommonParameter.CurrentUser.PersonId;
                mSelectedAccountingVoucher.LastModifiedDate = DateTime.Now;
                bool successful = FinAccountingVoucherBs.UpdateFinAccountingVoucher(mSelectedAccountingVoucher);
                if (successful)
                {
                    MessageBox.Show("删除成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        protected override void Click_Approve()
        {
            if (mSelectedAccountingVoucher == null)
            {
                MessageBox.Show("请选择一个记账凭证！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainAccountingVoucher frm = new frmMaintainAccountingVoucher();
            frm.ThisActionType = ActionType.Approve;
            frm.SelectedAccountingVoucher = mSelectedAccountingVoucher;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_Modify()
        {
            if (mSelectedAccountingVoucher == null)
            {
                MessageBox.Show("请选择一个记账凭证！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainAccountingVoucher frm = new frmMaintainAccountingVoucher();
            frm.ThisActionType = ActionType.Modify;
            frm.SelectedAccountingVoucher = mSelectedAccountingVoucher;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_New()
        {
            frmMaintainAccountingVoucher frm = new frmMaintainAccountingVoucher();
            frm.ThisActionType = ActionType.New;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }

        protected override void Click_Print()
        {
            //if (mSelectedAccountingVoucher == null)
            //{
            //    MessageBox.Show("请选择一个记账凭证！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    return;
            //}
            //List<FinAccountingVoucherEntity> list = new List<FinAccountingVoucherEntity>();
            //list.Add(mSelectedAccountingVoucher);
            //frmFindPrinter frmPrinter = new frmFindPrinter();
            //if (frmPrinter.ShowDialog() == DialogResult.OK)
            //{
            //    frmPrintAccountingVoucher frm = new frmPrintAccountingVoucher(list);
            //    frm.SelectedPrinterName = frmPrinter.SelectedPrinterName;
            //    frm.Print();
            //}
        }

        private void txtCreatedBy_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainEmployeeQuery frm = new frmMaintainEmployeeQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedPerson != null)
            {
                txtCreatedBy.Text = frm.SelectedPerson.NameCn;
                txtCreatedBy.Tag = frm.SelectedPerson;
            }
            else
            {
                txtCreatedBy.Text = "";
                txtCreatedBy.Tag = null;
            }
        }

        #endregion
    }
}
